package com.xdr.core.model;

import java.util.Date;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;



@Entity
@Table(name = "Users")
@AttributeOverride(name = "id", column = @Column(name = "UserId"))
public class User extends BaseEntity{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -903127818781561160L;

	@Column(name = "CreatedOn")
	private Date createdOn;

	@Column(name = "Email")
	private String email;

	@Column(name = "IsDeleted")
	private boolean isDeleted;
	
	@Column(name = "IsEmailConfirmed")
	private boolean isEmailConfirmed;

	@Column(name = "LastPasswordFailure")
	private Date lastPasswordFailure;

	@Column(name = "ModifiedOn")
	private Date modifiedOn;

	@Column(name = "PasswordFailuresSinceLastLogin")
	private int passwordFailuresSinceLastLogin;

	@Column(name = "PasswordHash")
	private String passwordHash;

	@Column(name = "PasswordVerificationToken")
	private String passwordVerificationToken;

	@Column(name = "PasswordVerificationTokenExpiration")
	private Date passwordVerificationTokenExpiration;

	@Column(name = "UserName")
	private String userName;

	@OneToMany(mappedBy="id",fetch = FetchType.LAZY)
	private List<Role> roles;

	public List<Company> getCompanies() {
		return companies;
	}

	public void setCompanies(List<Company> companies) {
		this.companies = companies;
	}


	@OneToMany(mappedBy="createdBy", cascade = CascadeType.ALL)
	private List<User> created;
	
	@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE},fetch = FetchType.LAZY)
	@JoinColumn(name="created", nullable=true, unique=false)
	private User createdBy;

	@OneToMany(mappedBy="modifiedBy", cascade = CascadeType.ALL)
	private List<User> modified;
	
	@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE},fetch = FetchType.LAZY)
	@JoinColumn(name="modified", nullable=true, unique=false)
	private User modifiedBy;

	
	@OneToMany(mappedBy="createdBy",fetch = FetchType.LAZY)
	private List<Company> companies;

	public Date getCreatedOn() {
		return createdOn;
	}

	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean isDeleted() {
		return isDeleted;
	}

	public void setDeleted(boolean isDeleted) {
		this.isDeleted = isDeleted;
	}

	public boolean isEmailConfirmed() {
		return isEmailConfirmed;
	}

	public void setEmailConfirmed(boolean isEmailConfirmed) {
		this.isEmailConfirmed = isEmailConfirmed;
	}

	public Date getLastPasswordFailure() {
		return lastPasswordFailure;
	}

	public void setLastPasswordFailure(Date lastPasswordFailure) {
		this.lastPasswordFailure = lastPasswordFailure;
	}

	public Date getModifiedOn() {
		return modifiedOn;
	}

	public void setModifiedOn(Date modifiedOn) {
		this.modifiedOn = modifiedOn;
	}

	public int getPasswordFailuresSinceLastLogin() {
		return passwordFailuresSinceLastLogin;
	}

	public void setPasswordFailuresSinceLastLogin(int passwordFailuresSinceLastLogin) {
		this.passwordFailuresSinceLastLogin = passwordFailuresSinceLastLogin;
	}

	public String getPasswordHash() {
		return passwordHash;
	}

	public void setPasswordHash(String passwordHash) {
		this.passwordHash = passwordHash;
	}

	public String getPasswordVerificationToken() {
		return passwordVerificationToken;
	}

	public void setPasswordVerificationToken(String passwordVerificationToken) {
		this.passwordVerificationToken = passwordVerificationToken;
	}

	public Date getPasswordVerificationTokenExpiration() {
		return passwordVerificationTokenExpiration;
	}

	public void setPasswordVerificationTokenExpiration(
			Date passwordVerificationTokenExpiration) {
		this.passwordVerificationTokenExpiration = passwordVerificationTokenExpiration;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public List<User> getCreated() {
		return created;
	}

	public void setCreated(List<User> created) {
		this.created = created;
	}

	public User getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(User createdBy) {
		this.createdBy = createdBy;
	}

	public List<User> getModified() {
		return modified;
	}

	public void setModified(List<User> modified) {
		this.modified = modified;
	}

	public User getModifiedBy() {
		return modifiedBy;
	}

	public void setModifiedBy(User modifiedBy) {
		this.modifiedBy = modifiedBy;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((modifiedBy == null) ? 0 : modifiedBy.hashCode());
		result = prime * result
				+ ((companies == null) ? 0 : companies.hashCode());
		result = prime * result + ((created == null) ? 0 : created.hashCode());
		result = prime * result
				+ ((createdBy == null) ? 0 : createdBy.hashCode());
		result = prime * result
				+ ((createdOn == null) ? 0 : createdOn.hashCode());
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + (isDeleted ? 1231 : 1237);
		result = prime * result + (isEmailConfirmed ? 1231 : 1237);
		result = prime
				* result
				+ ((lastPasswordFailure == null) ? 0 : lastPasswordFailure
						.hashCode());
		result = prime * result
				+ ((modified == null) ? 0 : modified.hashCode());
		result = prime * result
				+ ((modifiedOn == null) ? 0 : modifiedOn.hashCode());
		result = prime * result + passwordFailuresSinceLastLogin;
		result = prime * result
				+ ((passwordHash == null) ? 0 : passwordHash.hashCode());
		result = prime
				* result
				+ ((passwordVerificationToken == null) ? 0
						: passwordVerificationToken.hashCode());
		result = prime
				* result
				+ ((passwordVerificationTokenExpiration == null) ? 0
						: passwordVerificationTokenExpiration.hashCode());
		result = prime * result + ((roles == null) ? 0 : roles.hashCode());
		result = prime * result
				+ ((userName == null) ? 0 : userName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (modifiedBy == null) {
			if (other.modifiedBy != null)
				return false;
		} else if (!modifiedBy.equals(other.modifiedBy))
			return false;
		if (companies == null) {
			if (other.companies != null)
				return false;
		} else if (!companies.equals(other.companies))
			return false;
		if (created == null) {
			if (other.created != null)
				return false;
		} else if (!created.equals(other.created))
			return false;
		if (createdBy == null) {
			if (other.createdBy != null)
				return false;
		} else if (!createdBy.equals(other.createdBy))
			return false;
		if (createdOn == null) {
			if (other.createdOn != null)
				return false;
		} else if (!createdOn.equals(other.createdOn))
			return false;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (isDeleted != other.isDeleted)
			return false;
		if (isEmailConfirmed != other.isEmailConfirmed)
			return false;
		if (lastPasswordFailure == null) {
			if (other.lastPasswordFailure != null)
				return false;
		} else if (!lastPasswordFailure.equals(other.lastPasswordFailure))
			return false;
		if (modified == null) {
			if (other.modified != null)
				return false;
		} else if (!modified.equals(other.modified))
			return false;
		if (modifiedOn == null) {
			if (other.modifiedOn != null)
				return false;
		} else if (!modifiedOn.equals(other.modifiedOn))
			return false;
		if (passwordFailuresSinceLastLogin != other.passwordFailuresSinceLastLogin)
			return false;
		if (passwordHash == null) {
			if (other.passwordHash != null)
				return false;
		} else if (!passwordHash.equals(other.passwordHash))
			return false;
		if (passwordVerificationToken == null) {
			if (other.passwordVerificationToken != null)
				return false;
		} else if (!passwordVerificationToken
				.equals(other.passwordVerificationToken))
			return false;
		if (passwordVerificationTokenExpiration == null) {
			if (other.passwordVerificationTokenExpiration != null)
				return false;
		} else if (!passwordVerificationTokenExpiration
				.equals(other.passwordVerificationTokenExpiration))
			return false;
		if (roles == null) {
			if (other.roles != null)
				return false;
		} else if (!roles.equals(other.roles))
			return false;
		if (userName == null) {
			if (other.userName != null)
				return false;
		} else if (!userName.equals(other.userName))
			return false;
		return true;
	}

	public User(Date createdOn, String email, boolean isDeleted,
			boolean isEmailConfirmed, Date lastPasswordFailure,
			Date modifiedOn, int passwordFailuresSinceLastLogin,
			String passwordHash, String passwordVerificationToken,
			Date passwordVerificationTokenExpiration, String userName,
			List<Role> roles, User createdBy, User modifiedBy,
			List<Company> companies) {
		super();
		this.createdOn = createdOn;
		this.email = email;
		this.isDeleted = isDeleted;
		this.isEmailConfirmed = isEmailConfirmed;
		this.lastPasswordFailure = lastPasswordFailure;
		this.modifiedOn = modifiedOn;
		this.passwordFailuresSinceLastLogin = passwordFailuresSinceLastLogin;
		this.passwordHash = passwordHash;
		this.passwordVerificationToken = passwordVerificationToken;
		this.passwordVerificationTokenExpiration = passwordVerificationTokenExpiration;
		this.userName = userName;
		this.roles = roles;
		this.createdBy = createdBy;
		this.modifiedBy = modifiedBy;
		this.companies = companies;
	}
	
}
